from extensions import db
from datetime import datetime
from sqlalchemy.types import Numeric

class Warehouse(db.Model):
    __tablename__ = 'warehouses'
    
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), nullable=False)
    location = db.Column(db.String(200), nullable=False)
    capacity = db.Column(Numeric(10, 2), nullable=False)  # 容量（立方米）
    temperature = db.Column(Numeric(5, 2), nullable=False)  # 温度（摄氏度）
    humidity = db.Column(Numeric(5, 2), nullable=False)  # 湿度（%）
    status = db.Column(db.String(20), default='normal')  # normal, maintenance, full
    notes = db.Column(db.Text)

class Storage(db.Model):
    __tablename__ = 'storages'
    
    id = db.Column(db.Integer, primary_key=True)
    warehouse_id = db.Column(db.Integer, db.ForeignKey('warehouses.id'), nullable=False)
    sample_code = db.Column(db.String(50), unique=True, nullable=False)
    sample_type = db.Column(db.String(50), nullable=False)
    quantity = db.Column(Numeric(10, 2), nullable=False)
    storage_location = db.Column(db.String(50), nullable=False)  # 货架号-层号-位置号
    temperature_requirement = db.Column(Numeric(5, 2), nullable=False)
    humidity_requirement = db.Column(Numeric(5, 2), nullable=False)
    status = db.Column(db.String(20), default='stored')  # stored, in_transit, delivered
    entry_time = db.Column(db.DateTime, default=datetime.now)
    exit_time = db.Column(db.DateTime)
    operator_id = db.Column(db.Integer, db.ForeignKey('users.id'))

class Transport(db.Model):
    __tablename__ = 'transports'
    
    id = db.Column(db.Integer, primary_key=True)
    storage_id = db.Column(db.Integer, db.ForeignKey('storages.id'), nullable=False)
    vehicle_number = db.Column(db.String(20), nullable=False)
    driver_name = db.Column(db.String(50), nullable=False)
    driver_phone = db.Column(db.String(20), nullable=False)
    start_location = db.Column(db.String(200), nullable=False)
    end_location = db.Column(db.String(200), nullable=False)
    start_time = db.Column(db.DateTime, nullable=False)
    estimated_arrival_time = db.Column(db.DateTime, nullable=False)
    actual_arrival_time = db.Column(db.DateTime)
    status = db.Column(db.String(20), default='preparing')  # preparing, in_transit, completed
    current_temperature = db.Column(Numeric(5, 2), nullable=True)  # 允许为空
    current_humidity = db.Column(Numeric(5, 2), nullable=True)    # 允许为空
    notes = db.Column(db.Text) 